import Router from 'koa-router'

const { userValidator, verifyUser, userIdisExist, addUserValidator, verifyLogin } = require('../middleware/user.middleware')
const { auth } = require('../middleware/auth.middleware')
const { login, getCaptcha, getUsers, addUser, updateUser, getUserById, deleteUser, searchUsersByname } = require('../controller/user.controller') 
const router = new Router({ prefix: '/users' })

/**
 * 登录模块
 * @param login
 */
router.get('/getCaptcha', getCaptcha) // 获取验证码
// router.post('/register', userValidator, verifyUser, cryptPassword, register)
router.post('/login', userValidator, verifyLogin, login) // 登录

/**
 * 普通用户管理模块
 * @description users
 */
router.get('/', getUsers) // 获取所有普通用户(分页可选)
router.get('/name', searchUsersByname) // 根据用户名搜索用户
router.get('/:id', userIdisExist, getUserById) // 根据id获取用户
router.post('/', auth, addUserValidator, verifyUser, addUser) // 添加用户
router.put('/:id', auth, userIdisExist, updateUser) // 根据id更新用户
router.delete('/:id', auth, userIdisExist, deleteUser) // 根据id删除用户

module.exports = router